package com.chance.cc.crawler.prod.command.job.domain.vm.autohome.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.autohome.AutoHomeCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPageItem;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @ClassName song
 * @Description TODO
 * @Author ding
 * @Date 2021/9/18 16:56
 * @Version 1.0
 * 车家号文章  视频
 * 文章板块
 * 将放入redis读取  流转进 特定脚本中
 **/
public class AutoHomeSimpleRedisToRunCrawlerScheduleJob extends AutoHomeCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple";
    /**
     * 发布任务信息
     * @return
     */
    public static void main(String[] args) {
        publishAutoHomeCrawlerScheduleJobInfo();
    }
    public static CrawlerJob publishAutoHomeCrawlerScheduleJobInfo(){

        CrawlerJob autoHomeScheduler = autohomeScheduleCarHomeArticle();//车家号文章
       // CrawlerJob autoHomeScheduler = autohomeScheduleCarHomeVideo();//车家号视频
        //CrawlerJob autoHomeScheduler=autohomeArticle();//文章板块
        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(autoHomeScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("汽车之家临时采集任务");
        crawlerScheduleJob.setCrawlerKey(autoHomeScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return autoHomeScheduler;
    }
    //车家号文章
    public static CrawlerJob autohomeScheduleCarHomeArticle(){
        String site = "che_article";
        String site_biz = "simple";
        String site_info = "simple-20210922-20211102";
        String redisQueueName = "crawler-autohome-che_article1-simple-queue";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .domain(domain)
                .httpUrl("https://chejiahao.autohome.com.cn/info/9316620")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*52,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);


        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//autohome-che_article-simple-simple-20210922-simple-realtime
                        StringUtils.joinWith("-",site,site_biz,site_info,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(redisQueueName))
                .fileResultPipeline("kafka","/data/chance_crawler_runner/logs/node/autoArticle.log",false)
                .kafkaResultPipeline("kafka",kafkaTopciForTraceJob,null)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .build();

        //添加评论去重信息
        CrawlerRecord filterCrawlerRecord = new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,"comment")));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*52,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());
        return crawlerJob;
    }

    //车家号视频
    public static CrawlerJob autohomeScheduleCarHomeVideo(){
        String site = "che_video";
        String site_biz = "simple";
        String site_info = "simple-20210922-20211110";
        String redisQueueName = "crawler-autohome-che_video-simple-queue";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .domain(domain)
                .httpUrl("https://chejiahao.autohome.com.cn/info/9755112")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*52,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);

        CrawlerRecord crawlerRecord = new CrawlerRecord();
        crawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        crawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*52,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info",JSON.toJSONString(crawlerRecord));

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,site_biz,site_info,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(redisQueueName))
                .kafkaResultPipeline("kafka",kafkaTopciForTraceJob,null)
                .fileResultPipeline("kafka","/data/chance_crawler_runner/logs/node/che_video.log",false)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .build();
        //添加评论去重信息
        CrawlerRecord filterCrawlerRecord = new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,"comment")));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*52,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());
        return crawlerJob;
    }
    //文章视频
    public static CrawlerJob autohomeArticle(){
        String site = "article";
        String site_biz = "simple";
        String site_info = "simple-20210930";
        String redisQueueName = "crawler-autohome-article-simple-queue";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .domain(domain)
                .httpUrl("https://chejiahao.autohome.com.cn/info/9515639")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*62,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,site_biz,site_info,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(redisQueueName))
                .fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/autoArticle.log",false)
                .kafkaResultPipeline(null,kafkaTopciForTraceJob,null)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .build();
    }


}
